package com.android.update.up;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.update.config.HookConfig;
import com.android.update.config.HookDB;
import com.android.update.data.DataInfo;
import com.android.update.tool.ViewUtils;

import java.util.List;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class UpUtils {

    public static void authorization(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        //如果没有激活 就不拦截所有的东西
        if (!HookConfig.getIntance().isActivate()) {
            return;
        }
        String packName = lpparam.packageName;

        DataInfo info = HookDB.getIntance().getDataInfo();
        if (info.activitys.containsKey(packName)) {
            intercept(lpparam, info);
            XposedBridge.log("yj_hook_main->authorization" + packName);
        }
//        if (info.dialogs.containsKey(packName)) {
        interceptDialog(lpparam, info.dialogs.get(packName));
//        }
        XposedBridge.log("yj_hook_main->杨吉:package:" + packName);
//        if (info.activitys.containsKey(packName))
        hookGetActivity(info, lpparam);

        try{
            XposedHelpers.findAndHookMethod("com.qihoo.appstore.preference.PreferenceActivity",lpparam.classLoader,"onCreate",Bundle.class,new XC_MethodHook(){
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    XposedBridge.log("yj_hook_main->杨吉" + param.thisObject.getClass().getName());
                }
            });
        }catch (Exception e){}

//        XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
//            @Override
//            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                registerActivity((Application) param.thisObject);
//            }
//        });

//        if (info.activitys.containsKey(packName)) {
//            XposedHelpers.findAndHookMethod("android.app.Application", lpparam.classLoader, "onCreate", new XC_MethodHook() {
//                @Override
//                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                    registerActivity((Application) param.thisObject);
//                }
//            });
//            XposedHelpers.findAndHookMethod(Application.class, "onCreate", new XC_MethodHook() {
//                @Override
//                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                    super.beforeHookedMethod(param);
//                    Application app = (Application) param.thisObject;
//                    registerActivity(app);
//                }
//            });
//        }

    }


    //dialog 更新问题
    private static void interceptDialog(XC_LoadPackage.LoadPackageParam lpparam, final List<String> texts) {

        try {
            XposedHelpers.findAndHookMethod("android.app.Dialog", lpparam.classLoader, "show", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    Dialog dialog = (Dialog) param.thisObject;
                    XposedBridge.log("yj_hook_main->更新Dialog" + dialog.getContext().getPackageName());
                    View viewGroup = dialog.getWindow().getDecorView();
                    List<TextView> viewList = ViewUtils.findTextViews(viewGroup);
                    String text;
                    for (TextView tv : viewList) {
                        text = tv.getText().toString();
                        if (TextUtils.isEmpty(text)) {
                            continue;
                        }
                        for (String str : texts) {
                            XposedBridge.log("yj_hook_main->更新问题" + str);
                            if (TextUtils.isEmpty(str)) {
                                continue;
                            }
                            if (text.contains(str)) {
                                dialog.dismiss();
                            }
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //activity 更新问题
    private static void intercept(XC_LoadPackage.LoadPackageParam lpparam, DataInfo info) {
        String packName = lpparam.packageName;
        for (String className : info.activitys.get(packName)) {
            try {
                XposedBridge.log("yj_hook_main->更新问题" + packName + "：" + className);
                XposedHelpers.findAndHookMethod(className, lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("yj_hook_main->更新问题:" + param.thisObject.getClass().getName());
                        try {
                            ((Activity) param.thisObject).finish();
                        } catch (Exception e) {
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                XposedHelpers.findAndHookMethod(className, lpparam.classLoader, "onResume", new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        XposedBridge.log("yj_hook_main->更新问题:" + param.thisObject.getClass().getName());
                        try {
                            ((Activity) param.thisObject).finish();
                        } catch (Exception e) {
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void hookGetActivity(final DataInfo info, XC_LoadPackage.LoadPackageParam lpparam) {
        try {
            XposedHelpers.findAndHookMethod("android.app.Activity", lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    try {
                        Activity act = (Activity) param.thisObject;

                        XposedBridge.log("yj_hook_main->更新Activity:" + param.thisObject.getClass().getName());
                        XposedBridge.log("yj_hook_main->更新Activity1:" + JSON.toJSONString(info.activitys));
                        XposedBridge.log("yj_hook_main->更新Activity6:" + info.activitys.containsValue(param.thisObject.getClass().getName()));
                        XposedBridge.log("yj_hook_main->更新Activity7:" + act.getPackageName());
                        XposedBridge.log("yj_hook_main->更新Activity11:" + info.activitys.containsKey(act.getPackageName()));
                        if (info.activitys.containsKey(act.getPackageName()) && info.activitys.containsValue(param.thisObject.getClass().getName())) {
                            XposedBridge.log("yj_hook_main->更新Activity2:" + param.thisObject.getClass().getName());
                            act.finish();
                        }
                        for (String className : info.activitys.get(act.getPackageName())) {
                            XposedBridge.log("yj_hook_main->更新Activity4:" + param.thisObject.getClass().getName());
                            if (className.equals(param.thisObject.getClass().getName())) {
                                XposedBridge.log("yj_hook_main->更新Activity5:" + param.thisObject.getClass().getName());
                                act.finish();
                            }
                        }
                        XposedBridge.log("yj_hook_main->更新Activity3:" + param.thisObject.getClass().getName());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
